package com.aco.party.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.output.FileWriterWithEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by flying on 3/10/2016 0010.
 */
public class FreemarkerUtil {
    private static Logger logger = LoggerFactory.getLogger(FreemarkerUtil.class);

    public static void main(String[] args){
        String poName = "Links";
//        generateDao(poName);
//        generateMapper(poName);
//        generateService(poName);
        generateCommand(poName);
        generateVo(poName);
        generateValidator(poName);
        generateController(poName);
        logger.info("生成完毕！");
    }

    private static void generateController(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "controller";
        String targetPath = "/src/main/java/com/aco/party/web/controller/system/";
        String javaName = "Manage" + poName + "Controller";
        generate(root, templateName, targetPath, javaName);

    }

    private static void generateCommand(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "command";
        String targetPath = "/src/main/java/com/aco/party/web/command/system/";
        String javaName = "Edit" + poName + "Command";
        generate(root,templateName,targetPath,javaName);

    }

    private static void generateValidator(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "validator";
        String targetPath = "/src/main/java/com/aco/party/web/validator/system/";
        String javaName = "Edit" + poName + "Validator";
        generate(root,templateName,targetPath,javaName);

    }

    private static void generateService(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "service";
        String targetPath = "/src/main/java/com/aco/party/service/system/";
        String javaName = poName + "Service";
        generate(root,templateName,targetPath,javaName);

    }

    private static void generateDao(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "dao";
        String targetPath = "/src/main/java/com/aco/party/dao/custom/";
        String javaName = poName + "Dao";
        generate(root,templateName,targetPath,javaName);

    }

    private static void generateMapper(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "mapper";
        String targetPath = "/src/main/resources/mybatis/mapper/custom/";
        String javaName = poName + "Mapper";
        generate(root,templateName,targetPath,javaName,".xml");

    }

    private static void generateVo(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());

        String templateName = "vo";
        String targetPath = "/src/main/java/com/aco/party/model/vo/";
        String javaName = poName + "Vo";
        generate(root,templateName,targetPath,javaName);

    }

    private static void generateList(String poName,String menuName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());
        root.put("menuName",menuName);
        String templateName = "list";
        String targetPath = "/src/main/webapp/system/";
        String javaName = "manage" + poName + "s";
        generate(root,templateName,targetPath,javaName,".ftl");

    }

    private static void generateEdit(String poName){
        Map<String,Object> root = new HashMap<>();
        root.put("poNameC", poName);
        root.put("poName", poName.toLowerCase());
        String templateName = "edit";
        String targetPath = "/src/main/webapp/system/";
        String javaName = "edit" + poName;
        generate(root,templateName,targetPath,javaName,".ftl");

    }

    private static void generate(Map root,String templateName,String targetPath,String javaName,String extName){
        Configuration cfg = new Configuration();
        String tempPath = System.getProperty("user.dir") + "/src/main/webapp/template/";
        targetPath = System.getProperty("user.dir") + targetPath;
        try {
            cfg.setDirectoryForTemplateLoading(new File(tempPath));
            Template template = cfg.getTemplate(templateName + ".ftl");
            FileWriterWithEncoding fileOut = new FileWriterWithEncoding(targetPath + javaName + extName, Charset.forName("utf-8"));
            template.process(root, fileOut);

        } catch (IOException e) {
            System.out.println("Cause==>" + e);
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    private static void generate(Map root,String templateName,String targetPath,String javaName){
        generate(root,templateName,targetPath,javaName,".java");
    }
}
